﻿namespace IOLib
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class DynamicLabel : Control
    {
        private Bitmap _bmp;
        private Graphics _graphics;
        private int delta;
        private int inc;
        private Timer marqueue = new Timer();
        private int stopTime;
        private bool toLeft;

        public DynamicLabel()
        {
            this.marqueue.Interval = 50 / GlobalParams.ScreenFactor(1);
            this.marqueue.Tick += new EventHandler(this.marqueue_Tick);
            this.Text = string.Empty;
            base.Size = new Size(50, 20);
            this.stopTime = GlobalParams.ScreenFactor(10);
            this.toLeft = false;
            this.inc = 0;
            base.TabStop = false;
        }

        protected override void Dispose(bool disposing)
        {
            if (this.marqueue != null)
            {
                this.marqueue.Enabled = false;
            }
            base.Dispose(disposing);
        }

        private void marqueue_Tick(object sender, EventArgs e)
        {
            if (this.marqueue.Enabled)
            {
                if ((this.toLeft && (this.inc <= this.delta)) || (!this.toLeft && (this.inc >= 0)))
                {
                    this.stopTime--;
                    if (this.stopTime >= 0)
                    {
                        return;
                    }
                }
                if (this.toLeft && (this.inc <= this.delta))
                {
                    this.toLeft = false;
                }
                else if (!this.toLeft && (this.inc >= 0))
                {
                    this.toLeft = true;
                }
                if (this.toLeft)
                {
                    this.inc--;
                }
                else
                {
                    this.inc++;
                }
                this.stopTime = GlobalParams.ScreenFactor(10);
            }
            base.Invalidate();
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            if (this.marqueue != null)
            {
                this.marqueue.Enabled = false;
            }
            base.OnHandleDestroyed(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if ((this._graphics == null) || ((((this._bmp == null) || (this._bmp.Width != base.Width)) || (this._bmp.Height != base.Height)) && ((e.ClipRectangle.Width > 0) && (e.ClipRectangle.Height > 0))))
            {
                this._bmp = new Bitmap(base.Width, base.Height);
                this._graphics = Graphics.FromImage(this._bmp);
            }
            this._graphics.Clear(base.BackColor);
            this._graphics.DrawString(this.Text, base.Font, new SolidBrush(base.ForeColor), (float) this.inc, 0f);
            e.Graphics.DrawImage(this._bmp, 0, 0);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }

        protected override void OnResize(EventArgs e)
        {
            this.OnTextChanged(null);
            base.OnResize(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            int width = (int) Graphics.FromImage(new Bitmap(1, 1)).MeasureString(this.Text, base.Font).Width;
            this.delta = (base.Width - 2) - width;
            this.marqueue.Enabled = this.delta < 0;
            this.inc = 0;
            this.toLeft = false;
            base.Invalidate();
            base.OnTextChanged(e);
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }
    }
}

